import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AuthGuard } from './common/auth/auth.guard';
import { Logger } from '@nestjs/common';
import config from '../config';
import { TransformInterceptor } from '@/common/interceptors/transform.interceptor';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: ['error', 'warn', 'log', 'debug', 'verbose'],
  });
  // 定义全局日志记录器
  const logger = new Logger('Bootstrap');
  // 开启跨域
  app.enableCors();
  // 设置全局请求前缀
  app.setGlobalPrefix('api');
  // 打开swagger
  const swiggerConfig = new DocumentBuilder()
    .setTitle('pmp-cloud')
    .setDescription('pmp-cloud')
    .setVersion('1.0')
    .addBearerAuth()
    .addTag('API')
    .build();

  const document = SwaggerModule.createDocument(app, swiggerConfig);

  SwaggerModule.setup('api-swagger', app, document);
  // 全局守卫
  app.useGlobalGuards(new AuthGuard());

  // 返回值statusCode 设置为200
  // app.useGlobalInterceptors(new TransformInterceptor());

  logger.log('应用启动中');
  await app.listen(config.port);

  // 添加日志输出
  logger.log(`应用启动完成，访问地址: http://localhost:${config.port}/api`);
  logger.log(`接口文档: http://localhost:${config.port}/api-swagger`);
}

bootstrap();
